//
//  JWShopHomeVC.m
//  jinwang
//
//  Created by jzb on 2018/3/23.
//  Copyright © 2018年 jzb. All rights reserved.
//

#import "JWShopHomeVC.h"
#import "MainTabBarController.h"
#import "JWFBFacadeVCModel.h"
#import "JWShopHomeTableVC.h"

@interface JWShopHomeVC ()<LSXPopMenuDelegate>
{
    
    NSInteger tagsBGViewHeight;
}
//筛选
@property (nonatomic,strong) UIButton *shaixuanBtn;
@property (nonatomic,strong) UISearchBar *homeSearch;//搜索框

@property (nonatomic, strong) MLMSegmentHead *segHead;
@property (nonatomic, strong) MLMSegmentScroll *segScroll;
@property (nonatomic, strong) UIView *tagsBGView;

@property (nonatomic, strong) NSMutableArray *ListmodelArr,*list,*shopTagModelArr,*tagsBtnArr;


@end

@implementation JWShopHomeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.f) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"back_common"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(ChangeTabBar)];
    
    UIBarButtonItem *shaixuanItem = [[UIBarButtonItem alloc]initWithCustomView:self.shaixuanBtn];
    self.navigationItem.rightBarButtonItem = shaixuanItem;
    UIView *TitleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth-88, 44)];
//    TitleView.backgroundColor = [UIColor grayColor];
    UIButton *WaiBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 44, 44)];
//    WaiBtn.backgroundColor = [UIColor cyanColor];
    [WaiBtn setImage:[UIImage imageNamed:@"jw_shop_homeWaiDi_Icon"] forState:UIControlStateNormal];
    [WaiBtn addTarget:self action:@selector(titleBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    WaiBtn.tag = 2000;
    [TitleView addSubview:WaiBtn];
    UIButton *shopBtn = [[UIButton alloc]initWithFrame:CGRectMake(44, 0, 44, 44)];
//     shopBtn.backgroundColor = [UIColor cyanColor];
    [shopBtn setImage:[UIImage imageNamed:@"jw_shop_homeSanJiao_Icon"] forState:UIControlStateNormal];
    [shopBtn setTitle:@"商品" forState:UIControlStateNormal];
    [shopBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [shopBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -25, 0, 0)];
    [shopBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 35, 0, 0)];
    shopBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    shopBtn.tag = 2001;
    [shopBtn addTarget:self action:@selector(titleBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    [TitleView addSubview:shopBtn];
    _homeSearch = [[UISearchBar alloc]initWithFrame:CGRectMake(90, 0, kScreenWidth-90-90, 44)];
    _homeSearch.placeholder = @"输入搜索内容";
    _homeSearch.tintColor = UIColorHex(0xEEA72B);
    _homeSearch.searchBarStyle = UISearchBarStyleProminent;
    _homeSearch.barTintColor = kRGBColor(245, 245, 245);
    UITextField *searchField = [_homeSearch valueForKey:@"searchField"];
    
    if (searchField) {
        [searchField setBackgroundColor:[UIColor whiteColor]];
        searchField.layer.cornerRadius = (kScreenWidth-90-90)/11;//设置圆角具体根据实际情况来设置
//        searchField.layer.borderColor = [UIColor lightGrayColor].CGColor;//边框的颜色
//        searchField.layer.borderWidth = 1;//边框的宽
        [searchField setValue:[UIFont boldSystemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
        searchField.layer.masksToBounds = YES;
    }
    UIImageView *barImageView = [[[_homeSearch.subviews firstObject] subviews] firstObject];
    barImageView.layer.borderColor = kRGBAColor(248,248,248,1).CGColor;
    barImageView.layer.borderWidth = 1;
    [TitleView addSubview:_homeSearch];
    self.navigationItem.titleView = TitleView;
    
    [self makeUrl_mall_shopType];

}
- (void)segHeadViewBuild{
    
    _list = [NSMutableArray new];
    
    for (int i = 0; i < _ListmodelArr.count; i++) {
        
        JWFBFacadeVCModel *model = _ListmodelArr[i];
        
        [_list addObject:model.type_name];
    }
    
//    NSArray *list = @[@"门面",@"公司",@"电商",@"小摊",@"自产"];
    _segHead = [[MLMSegmentHead alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth-60, 44) titles:_list headStyle:SegmentHeadStyleLine layoutStyle:MLMSegmentLayoutDefault];
    //    _segHead.fontScale = 1.1;
    _segHead.fontSize = 16;
    _segHead.deSelectColor = [UIColor whiteColor];
    _segHead.selectColor = kRGBColor(177, 72, 0);
    _segHead.lineScale = .5;
    _segHead.bottomLineHeight = 0;
    _segHead.lineColor = kRGBColor(177, 72, 0);
    //    _segHead.headColor = [UIColor clearColor];
    _segHead.headColor = UIColorHex(0xEEA72B);
    
    UIButton *BtnBGView = [[UIButton alloc]initWithFrame:CGRectMake(kScreenWidth-60, 0, 60, 44)];
    BtnBGView.backgroundColor = UIColorHex(0xEEA72B);
    [BtnBGView addTarget:self action:@selector(moreBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:BtnBGView];
    UIImageView *moreImage = [[UIImageView alloc]initWithFrame:CGRectMake(22.5, 15, 15, 15)];
    moreImage.image = [UIImage imageNamed:@"jw_shop_homeUpDoun_Icon"];
    [BtnBGView addSubview:moreImage];
    
    tagsBGViewHeight = 0;
    _tagsBGView = [[UIView alloc]initWithFrame:CGRectMake(0, 44, kScreenWidth, tagsBGViewHeight)];
    _tagsBGView.backgroundColor = kRGBColor(236, 175, 63);
    [self.view addSubview:_tagsBGView];
    _tagsBtnArr = [NSMutableArray new];
    NSInteger x = 0;
    NSInteger y = 0;
    
    for (NSInteger i=0; i<_shopTagModelArr.count; i++) {
        JWFBFacadeVCModel *model = _shopTagModelArr[i];
        NSString *str = model.tag_name;
        //            NSString *str = arr[i];
        CGSize size = [str sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16.0f]}];
        if (x + kScreenWidth/5 > self.view.frame.size.width) {
            x = 0;
            y = y + size.height+25+5; //15为两行之间的高度间隔
        }
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, y, kScreenWidth/5, size.height+25)];
        [button setTitle:str forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14.0f];
        [button setTitleColor:kRGBColor(128, 100, 35) forState:UIControlStateNormal];
        [button addTarget:self action:@selector(TagsBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 1000+i;

        [_tagsBGView addSubview:button];
        x = x + kScreenWidth/5; //10为两个标签之间的宽度间隔
        tagsBGViewHeight = y + size.height+25;
        [_tagsBtnArr addObject:button];
    }
    
    _segScroll = [[MLMSegmentScroll alloc] initWithFrame:CGRectMake(0, _tagsBGView.bottom, kScreenWidth, kScreenHeight - ( _segHead.height + _tagsBGView.height)-64-49)  vcOrViews:[self vcArr:_list.count]];
    _segScroll.loadAll = YES;
    _segScroll.showIndex = 0;
    [MLMSegmentManager associateHead:_segHead withScroll:_segScroll completion:^{
        
        [self.view addSubview:_segHead];
        [self.view addSubview:_segScroll];
    }];
}
#pragma mark - 数据源
- (NSArray *)vcArr:(NSInteger)count {
    NSMutableArray *arr = [NSMutableArray array];
    for (int i = 0; i < _ListmodelArr.count; i++) {
        JWFBFacadeVCModel *model = _ListmodelArr[i];
        JWShopHomeTableVC *HomeTableVC = [[JWShopHomeTableVC alloc]init];
        HomeTableVC.shop_type = [model.type_id stringValue];
        //        FacadeTagVC.parent_id = _parent_id;
        
        HomeTableVC.view.backgroundColor = [UIColor cyanColor];
        [arr addObject:HomeTableVC];
    }
    return arr;
}
- (void)titleBtnOnclick:(UIButton *)sender{
    
    if (sender.tag == 2000) {
        
        [LSXPopMenu showRelyOnView:sender titles:@[@"区域",@"外来"]  icons:nil menuWidth:100 isShowTriangle:YES delegate:self];
        
    }else if (sender.tag == 2001){
        
        [LSXPopMenu showRelyOnView:sender titles:@[@"店铺",@"商品",@"服务"]  icons:nil menuWidth:100 isShowTriangle:YES delegate:self];
    }
}
- (void)shaixuanBtnOnclick:(UIButton *)sender{
    
}
- (void)moreBtnOnclick:(UIButton *)sender{
    
    sender.selected = !sender.selected;
    
    if (sender.selected) {
        
        _tagsBGView.frame = CGRectMake(0, 44, kScreenWidth, tagsBGViewHeight);
        
        _segScroll.frame = CGRectMake(0, _tagsBGView.bottom, kScreenWidth, kScreenHeight - ( _segHead.height + _tagsBGView.height)-64-49);
        
    }else{
        
        _tagsBGView.frame = CGRectMake(0, 44, kScreenWidth, 0);
        _segScroll.frame = CGRectMake(0, _tagsBGView.bottom, kScreenWidth, kScreenHeight - ( _segHead.height + _tagsBGView.height)-64-49);
    }
}
- (void)TagsBtnOnclick:(UIButton *)sender{
    
    for (UIButton *button in _tagsBtnArr) {
        
        [button setTitleColor:kRGBColor(128, 100, 35) forState:UIControlStateNormal];
    }
    [sender setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
- (void)ChangeTabBar{
    
    MainTabBarController *tabBarC = [[MainTabBarController alloc]init];
    
    [tabBarC setSelectedIndex:3];
    kKeyWindow.rootViewController = tabBarC;
//    kKeyWindow.rootViewController
}
-(void)LSXPopupMenuDidSelectedAtIndex:(NSInteger)index LSXPopupMenu:(LSXPopMenu *)LSXPopupMenu{
    
    
}
#pragma mark -网络请求
//门店类型
-(void)makeUrl_mall_shopType{
    [[DDNetworkManagerDate makeUrl_mall_shopTypeWithtoken:k_user_token] post_RequestFinshSuccess:^(id responseObject) {
        
//        NSLog(@"-=-=>>%@",responseObject);
        if ([self requestFinshCode:responseObject isShowMessage:NO] == 1) {
            _ListmodelArr = [NSMutableArray new];
            NSArray *data = [responseObject objectForKey:kRequsetDataKey];
            //转模型
            NSMutableArray *arr = [NSMutableArray array];
            [data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                JWFBFacadeVCModel *model = [JWFBFacadeVCModel yy_modelWithDictionary:obj];
                [arr addObject:model];
            }];
            self.ListmodelArr = arr.mutableCopy;
            
            [self makeUrl_mall_shopTag];            
        }
        
    } failure:^(id errorObject) {
        
        [self showBottomMessag:@"网络请求失败"];
    }];
}
//商铺标签
-(void)makeUrl_mall_shopTag{
    [[DDNetworkManagerDate makeUrl_mall_shopTagWithtoken:k_user_token] post_RequestFinshSuccess:^(id responseObject) {
        
//        NSLog(@"-=-=>>%@",responseObject);
        if ([self requestFinshCode:responseObject isShowMessage:NO] == 1) {
            _shopTagModelArr = [NSMutableArray new];
            NSArray *data = [responseObject objectForKey:kRequsetDataKey];
            NSMutableArray *arr = [NSMutableArray array];
            [data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                JWFBFacadeVCModel *model = [JWFBFacadeVCModel yy_modelWithDictionary:obj];
                [arr addObject:model];
            }];
            self.shopTagModelArr = arr.mutableCopy;
            
            [self segHeadViewBuild];
        }
        
    } failure:^(id errorObject) {
        
        [self showBottomMessag:@"网络请求失败"];
    }];
}

#pragma mark -懒加载
- (UIButton *)shaixuanBtn{
    if (!_shaixuanBtn) {
        _shaixuanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _shaixuanBtn.frame = CGRectMake(0, 0, 44, 44);
        [_shaixuanBtn setImage:[UIImage imageNamed:@"jw_bojian_shaixuan"] forState:UIControlStateNormal];
        [_shaixuanBtn addTarget:self action:@selector(shaixuanBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _shaixuanBtn;
}
-(UISearchBar *)homeSearch{
    if (!_homeSearch) {
        _homeSearch = [[UISearchBar alloc]initWithFrame:CGRectMake(88, 0, kScreenWidth-88-88, 44)];
        _homeSearch.placeholder = @"搜索";
        _homeSearch.tintColor = UIColorHex(0xEEA72B);
        
        _homeSearch.searchBarStyle = UISearchBarStyleProminent;
        _homeSearch.barTintColor = kRGBColor(245, 245, 245);
        UIImageView *barImageView = [[[_homeSearch.subviews firstObject] subviews] firstObject];
        barImageView.layer.borderColor = kRGBAColor(248,248,248,1).CGColor;
        barImageView.layer.borderWidth = 1;
    }
    return _homeSearch;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
